Event.observe ( window, 'load', bootstrap );

function bootstrap () {
  $( 'specification' ).observe ( 'change', display_commits );
  
  if ( $( 'specification' ).value != '' ) {
    display_commits ();
  }
}

function display_commits () {
  var specification = $( 'specification' ).value;
  
  if ( specification != '' ) {
    new Ajax.Request ( '/rest/specification/' + specification + '/commits', {
      method         : 'get',
      requestHeaders : { Accept : 'application/json' },
      onCreate       : function () {
        $( 'commits_table'       ).replace ( '<div id="commits_table"></div>'   );
        $( 'isock_altnavigation' ).replace ( '<div id="isock_altnavigation" />' );
        
        display_progress ( 'Querying SVN...' );
      },
      onFailure      : function () {
        $( 'commits_table' ).replace ( '<p id="commits_table" class="error">No commits!</p>' );
      },
      onSuccess      : function ( transport ) {
        var commits = transport.responseJSON;

        if ( commits.length > 0 ) {
          $( 'commits_table' ).replace ( commits_as_html_table ( commits, 'commits_table', specification, '-', 'specification_id' ) );
          
          $( 'isock_altnavigation' ).replace (
              "<div id=\"isock_altnavigation\">\n"
            + "  <ul id=\"isock_altnavigation-index\">\n"
            + "    <li id=\"isock_altnavigation-index-toc\">Links</li>\n"
            + "    <li>Links</li>\n"
            + "    <li><a href=\"/specification/commits?id="  + specification + "\">Self</a></li>\n"
            + "  </ul>\n"
            + "</div>\n"
          );
        }
        else {
          $( 'commits_table' ).replace ( '<p id="commits_table" class="error">No commits!</p>' );
        }
      },
      onComplete     : function () {
        hide_progress ();
      }
    } );
  }
}
